package com.work.farm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.work.crops.feign.CropsDiseaseFeign;
import com.work.crops.feign.CropsInfoFeign;
import com.work.crops.pojo.CropsDisease;
import com.work.crops.pojo.CropsInfo;
import com.work.device.feign.DeviceInfoFeign;
import com.work.device.pojo.DeviceInfo;
import com.work.entity.PageResult;
import com.work.entity.Result;
import com.work.farm.dao.FarmLandCropsDao;
import com.work.farm.dao.FarmLandDao;
import com.work.farm.dao.FarmLandDeviceDao;
import com.work.farm.dao.HistoryDao;
import com.work.farm.pojo.TCustFarmlandCropsEntity;
import com.work.farm.pojo.TCustFarmlandDeviceEntity;
import com.work.farm.pojo.TCustFarmlandEntity;
import com.work.farm.pojo.TCustFarmlandHistoryEntity;
import com.work.farm.service.FarmLandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FarmLandServiceImpl implements FarmLandService {

    @Autowired
    private FarmLandDao farmLandDao;

    @Autowired
    private FarmLandCropsDao farmLandCropsDao;

    @Autowired
    private FarmLandDeviceDao farmLandDeviceDao;

    @Autowired
    private CropsInfoFeign cropsInfoFeign;

    @Autowired
    private DeviceInfoFeign deviceInfoFeign;

    @Autowired
    private CropsDiseaseFeign cropsDiseaseFeign;

    @Autowired
    private HistoryDao historyDao;

    //添加大田
    @Override
    public void addFarmLand(TCustFarmlandEntity tCustFarmlandEntity) {
        //设置大田状态为可可用
        tCustFarmlandEntity.setStatus("1");
        farmLandDao.insertSelective(tCustFarmlandEntity);
    }

    //删除大田
    @Override
    public void deleteFarmLand(Integer id) {
        //根据主键进行删除
        farmLandDao.deleteById(id);
    }

    //分页搜索查询所有大田信息
    @Override
    public Page<TCustFarmlandEntity> findByPage(Integer pageNum, Integer pageSize, Map searchMap) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = createExample(searchMap);
        return (Page<TCustFarmlandEntity>) farmLandDao.selectByExample(example);

    }

    //修改大田信息
    @Override
    public void updateFarmLand(TCustFarmlandEntity tCustFarmlandEntity) {
        farmLandDao.updateByPrimaryKeySelective(tCustFarmlandEntity);
    }

    //根据id查询大田的详细信息,展示农作物信息,展示设备指标,设备数量,害虫信息
    @Override
    public Map<String, Object> findById(Integer id) {
        //创建一个map集合进行封装参数
        Map<String,Object> map = new HashMap<>();

//根据id 查询大田的基本信息
        TCustFarmlandEntity tCustFarmlandEntity = farmLandDao.selectByPrimaryKey(id);
        map.put("farmland",tCustFarmlandEntity);

//根据大田id查询种植的农作物
        //创建条件构造对象
        Example example = new Example(TCustFarmlandCropsEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("fid",id);
        //得到大田中的农作物
        List<TCustFarmlandCropsEntity> tCustFarmlandCropsEntities = farmLandCropsDao.selectByExample(example);
        //用来存储农作物的相关信息
        ArrayList<CropsInfo> list = new ArrayList<>();
        ArrayList<Object> list2 = new ArrayList<>();
        for (TCustFarmlandCropsEntity tCustFarmlandCropsEntity : tCustFarmlandCropsEntities) {
            //获取农作物id
            Long cid = tCustFarmlandCropsEntity.getCid();
            //根据农作物id进行查询农作物详情信息
            CropsInfo cropsInfo = cropsInfoFeign.findCropsById(Integer.parseInt(cid.toString())).getData();
            //根据农作物id查询病虫害
            List<CropsDisease> cropsDiseases = cropsDiseaseFeign.findByCropsId(cid).getData();

            list.add(cropsInfo);

            list2.add(cropsDiseases);
        }
        //将得到的农作物详情放到map中
        map.put("crops",list);

        //将病虫害信息存储进map中
        map.put("cropsDisease",list2);

        map.put("farmlandCrops",tCustFarmlandCropsEntities);
//根据大田id查询设备安装
        //创建条件对象
        Example example1 = new Example(TCustFarmlandDeviceEntity.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("fid",id);
        List<TCustFarmlandDeviceEntity> tCustFarmlandDeviceEntities = farmLandDeviceDao.selectByExample(example1);
        ArrayList<DeviceInfo> list1 = new ArrayList<>();
        //遍历查询设备信息
        for (TCustFarmlandDeviceEntity tCustFarmlandDeviceEntity : tCustFarmlandDeviceEntities) {
            //获取设备id
            Long cdid = tCustFarmlandDeviceEntity.getCdid();
            //调用接口获取数据
            List<DeviceInfo> data = deviceInfoFeign.get(cdid).getData();
//            for (DeviceInfo datum : data) {
//                list1.add(datum);
//            }
            //将得到的设备数据进行存储
            map.put("deviceInfo",data);
        }

        //设备数量进行存储
        map.put("deviceNum",list1.size());


        map.put("farmlandDevice",tCustFarmlandDeviceEntities);


        return map;
    }

    //查询大田历史信息
    @Override
    public List<TCustFarmlandHistoryEntity> findHistory(Integer fid) {
        List<TCustFarmlandHistoryEntity> historyList=  historyDao.findAllByFid(fid);
        return historyList;
    }

    //构建一个私有的查询条件对象
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(TCustFarmlandEntity.class);
        Example.Criteria criteria = example.createCriteria();
        //根据id进行查询
        //判断需要的查询条件是否存在
        if (searchMap != null) {
            //所属的农场片区
            if (searchMap.get("fid") != null && !"".equals(searchMap.get("fid"))) {
                criteria.andEqualTo("fid", searchMap.get("fid"));
            }
            //大田编号
            if (searchMap.get("code") != null && !"".equals(searchMap.get("code"))) {
                criteria.andEqualTo("code", searchMap.get("code"));
            }
            //大田的形状
            if (searchMap.get("shape") != null && !"".equals(searchMap.get("shape"))) {
                criteria.andLike("shape", "%" + searchMap.get("shape") + "%");
            }
        }

        return example;
    }


}
